package org.edu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.edu.model.User;
import org.edu.model.UserDetailed;
import org.edu.service.EmpService;
import org.edu.service.RoleService;
import org.edu.service.UserService;
//import org.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
/**
 * <h2>新建用户Action</h2>
 * <p>已在方法上注明方法作用</p>
 * @author 谢圣强
 * @version 2.0
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/newuser")
public class NewUserAction extends ActionSupport implements RequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5340558274428317192L;
	private static final Logger logger = Logger.getLogger(NewUserAction.class);
	private Map<String, Object> request;
	private User user;
	private UserDetailed userd;
	private String roleid;
	private String aidroleid;
	private String empno;

	
	@Autowired
	private EmpService empService;
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;
	/**
	 * 新建用户方法 必须参数：<br>user 类属性<br>
	 * roleid 主角色id<br>
	 * empid 部门id<br>
	 * <br>
	 * 可选参数 :<br>UserDetailed 类属性<br>
	 * aidroleid 辅助角色<br>
	 * @return
	 * @throws Exception
	 */
	@Action(value="new",results={@Result(name="success",location="/examples/Staff.jsp")})
	public String newuser() throws Exception {
//		System.out.println(user);System.out.println(userd);System.out.println(roleid);System.out.println(aidroleid);
		try{
			logger.info("新建用户");
			userService.newUser(user, userd, roleid, aidroleid,empno);
		}catch (Exception e){
			logger.info("注册失败");
			request.put("tip", "注册失败");
		}
		return SUCCESS;
	}
	/**
	 * 获许注册页面信息
	 */
	@Action(value="signup",results={@Result(name="success",location="/examples/NewUser.jsp")})
	public String signup(){
		
		this.loadRole();//角色
		
		return SUCCESS;
	}
	/**
	 * 获取部门信息树
	 */
	@Action(value="loademp")
	public void loademp(){
		HttpServletResponse response = ServletActionContext.getResponse();
		// 以下代码从JSON.java中拷过来的
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(empService.list().toString());
		out.println(empService.list().toString());
		logger.info("获取全部部门列表");
		//request.put("emplist", empService.list());
	}
	@Action(value="loadrole")
	public void loadRole(){
		logger.info("获取角色列表");
		request.put("rolelist",roleService.list());
		
	}
	public Map<String, Object> getRequest() {
		return request;
	}
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserDetailed getUserd() {
		return userd;
	}
	public void setUserd(UserDetailed userd) {
		this.userd = userd;
	}
	public String getRoleid() {
		return roleid;
	}
	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
	public String getAidroleid() {
		return aidroleid;
	}
	public void setAidroleid(String aidroleid) {
		this.aidroleid = aidroleid;
	}
	public String getEmpno() {
		return empno;
	}
	public void setEmpno(String empno) {
		this.empno = empno;
	}



	
	
}
